package com.tensuqare.user.controller;

import com.tensuqare.user.pojo.User;
import com.tensuqare.user.service.UserService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private HttpServletRequest request;



    /**
     * 增加粉丝数
     */
    @RequestMapping(value = "/incfans/{userid}/{x}", method = RequestMethod.POST)
    public void incFanscount(@PathVariable String userid, @PathVariable int x) {
        userService.incFanscount(userid, x);
    }

    /**
     * 增加关注数
     */
    @RequestMapping(value = "/incfollow/{userid}/{x}", method = RequestMethod.POST)
    public void incFollowcount(@PathVariable String userid, @PathVariable int x) {
        userService.incFollowcount(userid, x);
    }

    @RequestMapping(value = "/sendsms/{mobile}",method = RequestMethod.POST)
    public Result sendsms(@PathVariable String mobile){
        userService.sendsms(mobile);
        return new Result(StatusCode.OK, true, "发送验证码成功");
    }

    @RequestMapping(value = "/register/{code}",method = RequestMethod.POST)
    public Result regist(@RequestBody User user,@PathVariable String code){
        String r_code = (String) redisTemplate.opsForValue().get(user.getMobile());
        if(!code.equals(r_code)){
            return new Result(StatusCode.OK, true,"验证码不正确，请重新发送验证码" );
        }
        userService.register(user);
        return new Result(StatusCode.OK, true,"注册成功" );
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(@RequestBody Map<String, String> loginMap) {
        User user = userService.finByMobileAndPassword(loginMap.get("mobile"), loginMap.get("password"));
        if (user != null) {
            String token = jwtUtil.createJWT(user.getId(), user.getNickname(), "user");
            Map<String, String> map = new HashMap<>();
            map.put("token", token);
            map.put("name", user.getNickname());
            map.put("avatar", user.getAvatar());
            return new Result(StatusCode.OK, true, "登陆成功", map);
        }
        return new Result(StatusCode.LOGINERROR, false, "账号或密码错误");
    }

    @RequestMapping(value = "/{userId}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable String userId){

        //用户认证
        // String authorization = request.getHeader("Authorization");
        // if(authorization!=null&&authorization.startsWith("Bearer ")){
        //     String token = authorization.substring(7);
        //     Claims claims = jwtUtil.parseJWT(token);
        //     if(claims!=null&&"admin".equals(claims.get("roles"))){
        //         userService.delete(userId);
        //         return new Result(StatusCode.OK, true,"删除成功" );
        //     }
        // }
        Claims claims = (Claims) request.getAttribute("admin_claims");
        if(claims!=null){
            userService.delete(userId);
            return new Result(StatusCode.OK, true,"删除成功" );
        }

        return new Result(StatusCode.ACCESSERROR, false, "权限不足");

    }
}
